using UnityEngine;
using System.Collections;
//////////////////////////////////////////////////////
///fps 鼠标控制摄影机
//////////////////////////////////////////////////////
[AddComponentMenu("Level4/Cameras/MouseLooker")]
public class MouseLooker : MonoBehaviour
{
    public string MOUSE_X = "Mouse X";
    public string MOUSE_Y = "Mouse Y";

    public float intensityX = 15f;
    public float intensityY = 15f;

    public float minimumX = -360f;
    public float maximumX = 360f;

    public float minimumY = -80f;
    public float maximumY = 80f;

    private float rotationX = 0f;
    private float rotationY = 0f;

    private Quaternion originalRotation;

    // Use this for initialization
    void Start()
    {
        originalRotation = transform.localRotation;
    }

    // Update is called once per frame
    void Update()
    {
        rotationX += Input.GetAxis(MOUSE_X) * intensityX;
        rotationY += Input.GetAxis(MOUSE_Y) * intensityY;

        rotationX = ClampAngle(rotationX, minimumX, maximumX);
        rotationY = ClampAngle(rotationY, minimumY, maximumY);

        Quaternion quaternionX = Quaternion.AngleAxis(rotationX, Vector3.up);
        Quaternion quaternionY = Quaternion.AngleAxis(rotationY, Vector3.left);

        transform.localRotation = originalRotation * quaternionX * quaternionY;
    }

    float ClampAngle(float angle, float min, float max)
    {
        if (angle > 360)
            angle -= 360;
        if (angle < -360)
            angle += 360;
        return Mathf.Clamp(angle, min, max);
    }
}
